package util.biz.excel;

import org.apache.poi.ss.usermodel.Sheet;
import util.model.common.FieldInfo;
import util.util.DevFileUtil;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: liW
 * @date: 2020/12/9 13:32
 */
public class Test01 {

  public static void main(String[] args) {
//    batchImportSubUser("D:\\lw\\30yzwl\\8c2a3ee2.xls");

    List<FieldInfo> list = new ArrayList<>();
    FieldInfo f = new FieldInfo();
    f.setTableName("1");
    list.add(f);

    f = new FieldInfo();
    f.setTableName("155");
    list.add(f);

    f = new FieldInfo();
    f.setTableName("11");
    list.add(f);

    f = new FieldInfo();
    f.setTableName("2");
    list.add(f);

    List<FieldInfo> collect = list.stream().sorted(
        Comparator.comparing(FieldInfo::getTableName, Comparator.comparingInt(String::length)))
        .collect(Collectors.toList());
    collect.forEach(System.out::println);


  }

  /**
   * 子账号--批量导入
   *
   * @param url 地址
   * @return 导入信息
   */
  public static void batchImportSubUser(String url) {

    Sheet sheet = DevFileUtil.getSheet(url, 0);

    // 配置导入字段
    ExcelConverConfig<FieldInfo> config = subUserImportconfig();
    // 读取excel数据
    List<ExcelCheckConfig.RowData<FieldInfo>> rowDataList = ExcelReadConfig.<FieldInfo>builder().config(config)
        .sheet(sheet).startRowNo(3).supplier(FieldInfo::new).stopFunction(FieldInfo::getTableName)
        .build().checkTitle(2).readSheet();

    // 校验
    ExcelCheckConfig<FieldInfo> repeatConfig = ExcelCheckConfig.<FieldInfo>builder()
        .rowDataList(rowDataList).config(config).build()
        .checkMust().checkExcelRepeat().checkLength().checkDbRepeat();

    // 批量插入
    List<FieldInfo> insertList = repeatConfig.getInsertList();

    for (FieldInfo fieldInfo : insertList) {
      System.out.println(fieldInfo);
    }
    String s = repeatConfig.toMsg();
    System.out.println(s);
  }

  /**
   * 子账号--批量导入--配置
   *
   * @return config 配置信息
   */
  private static ExcelConverConfig<FieldInfo> subUserImportconfig() {
    ExcelConverConfig<FieldInfo> config = new ExcelConverConfig<>();

    config.add(ExcelConverConfig.ExcelCell
        .getStrInstance("*员工编号", FieldInfo::getTableName, FieldInfo::setTableName,
            2, 20, true, true, true));
    config.add(ExcelConverConfig.ExcelCell
        .getStrInstance("*姓名", FieldInfo::getTableDBName, FieldInfo::setTableDBName,
            3, 45, false, false, true));
    config.add(ExcelConverConfig.ExcelCell
        .getStrInstance("*手机号码", FieldInfo::getTableTitle, FieldInfo::setTableTitle,
            4, 20, true, true, true));
    return config;
  }


}
